/* Notification.java
 *
 * Copyright 2011 Aleksey Konovalov
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 * 
 */

package org.jimm.service;

import org.jimm.app.R;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Vibrator;

public final class Notifications {

    private final static long VIBRATOR_PATTERN[] = {0, 300, 150, 140, 150, 140};

    private Vibrator vibrator;
    private Context context;
    private Preferences prefs;

    public Notifications(Context context, Preferences preferences) {
        this.context = context;
        prefs = preferences;

        vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    private void playSound(int resid) {
        if (prefs.soundEnabled) {
            MediaPlayer mp = MediaPlayer.create(context, resid);
            //	mp.start();
            if (mp.isPlaying())
                mp.seekTo(0);
            else mp.start();
        }
    }

    private void vibrate(long[] pattern) {
        if (vibrator != null && prefs.vibroEnabled)
            vibrator.vibrate(pattern, -1);
    }

    // on input text message
    public void onInputMessage() {
        playSound(R.raw.message_sound);
        vibrate(VIBRATOR_PATTERN);
    }

    public void onTyping() {
        //playSound(R.raw.startup_sound);
    }

    // on login
    public void onLogin() {
        playSound(R.raw.startup_sound);
    }

    public void onReconnect() {
        //playSound(R.raw.startup_sound);
    }

    public void onContactOnline() {
        playSound(R.raw.contact_online_sound);
    }

}
